<?php

/**
 * AqzintroductionController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class AqzintroductionController extends Zend_Controller_Action 
{
	protected $_qzintroductionModel;
	
	public function init() 
	{
	     $authService = new Eleven_Service_Authentication();
	           
	    if ($authService->getIdentity()->role != 'Teacher') {
	        $this->_helper->redirector('index','index');
	    }
	    
		$this->_qzintroductionModel = new Eleven_Model_Qzintroduction();
		
		$this->view->editForm = $this->getEditForm();
		
		$this->view->controller = $this->_request->controller;
	}
    /**
     * The default action - show the home page
     */
    public function indexAction ()
    {    
        
    }
    
    public function getEditForm()
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['edit'] = $this->_qzintroductionModel->getForm('qzintroductionEdit');
        $this->_forms['edit']->setAction($urlHelper->url(array(
            'controller'	=> 'aqzintroduction',
            'action' => 'save'
            )
        ));
        
        $this->_forms['edit']->setMethod('post');
        
        $introduction = $this->_qzintroductionModel->getIntroduction();
        
        $this->_forms['edit']->getElement('members')->setValue($introduction->members);
        $this->_forms['edit']->getElement('content')->setValue(stripslashes($introduction->content));
        $this->_forms['edit']->getElement('views')->setValue($introduction->views);
        $this->_forms['edit']->getElement('createdate')->setValue($introduction->createdate);
        $this->_forms['edit']->getElement('type')->setValue($introduction->type);
        $this->view->picpath = $introduction->filepath;
       
        return $this->_forms['edit'];
	}
	
    public function saveAction()
	{
		$request = $this->getRequest();

		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}
  		
  		if (false === $this->_qzintroductionModel->saveEdit($request->getPost())) 
  		{
     		return $this->render('index');
  		}
  		
  		return $this->_helper->redirector('index');
	}

}

